﻿using System;
using System.Collections.Generic;

using System.Text;
using System.Data.SqlClient;
using System.Data;
using Common;

namespace TimKen.DataAccess
{
    /// <summary>
    /// 数据访问类FavoriteDAO。
    /// </summary>
    public class FavoriteDAO
    {
        public FavoriteDAO()
        { }
        #region  成员方法

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("ProductID", "Favorite");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string UserName, int ProductID)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@UserName", SqlDbType.VarChar,50),
					new SqlParameter("@ProductID", SqlDbType.Int,4)};
            parameters[0].Value = UserName;
            parameters[1].Value = ProductID;

            int result = DbHelperSQL.RunProcedure("UP_Favorite_Exists", parameters, out rowsAffected);
            if (result == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        ///  增加一条数据
        /// </summary>
        public void Add(TimKen.DataEntity.Favorite model)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@UserName", SqlDbType.VarChar,32),
					new SqlParameter("@ProductID", SqlDbType.Int,4),
					new SqlParameter("@DateAdded", SqlDbType.DateTime)};
            parameters[0].Value = model.UserName;
            parameters[1].Value = model.ProductID;
            parameters[2].Value = model.DateAdded;

            DbHelperSQL.RunProcedure("UP_Favorite_ADD", parameters, out rowsAffected);
        }

        /// <summary>
        ///  更新一条数据
        /// </summary>
        public void Update(TimKen.DataEntity.Favorite model)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@UserName", SqlDbType.VarChar,32),
					new SqlParameter("@ProductID", SqlDbType.Int,4),
					new SqlParameter("@DateAdded", SqlDbType.DateTime)};
            parameters[0].Value = model.UserName;
            parameters[1].Value = model.ProductID;
            parameters[2].Value = model.DateAdded;

            DbHelperSQL.RunProcedure("UP_Favorite_Update", parameters, out rowsAffected);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(string UserName, int ProductID)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@UserName", SqlDbType.VarChar,50),
					new SqlParameter("@ProductID", SqlDbType.Int,4)};
            parameters[0].Value = UserName;
            parameters[1].Value = ProductID;

            DbHelperSQL.RunProcedure("UP_Favorite_Delete", parameters, out rowsAffected);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public TimKen.DataEntity.Favorite GetModel(string UserName, int ProductID)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@UserName", SqlDbType.VarChar,50),
					new SqlParameter("@ProductID", SqlDbType.Int,4)};
            parameters[0].Value = UserName;
            parameters[1].Value = ProductID;

            TimKen.DataEntity.Favorite model = new TimKen.DataEntity.Favorite();
            DataSet ds = DbHelperSQL.RunProcedure("UP_Favorite_GetModel", parameters, "ds");
            if (ds.Tables[0].Rows.Count > 0)
            {
                model.UserName = ds.Tables[0].Rows[0]["UserName"].ToString();
                if (ds.Tables[0].Rows[0]["ProductID"].ToString() != "")
                {
                    model.ProductID = int.Parse(ds.Tables[0].Rows[0]["ProductID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["DateAdded"].ToString() != "")
                {
                    model.DateAdded = DateTime.Parse(ds.Tables[0].Rows[0]["DateAdded"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select UserName,ProductID,DateAdded ");
            strSql.Append(" FROM Favorite ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }
        /// <summary>
        /// 获取总数
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public int getCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(*) UserName,ProductID,DateAdded ");
            strSql.Append(" FROM Favorite ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return Convert.ToInt32(SqlHelper.ExecuteScalar(DBSetting.ConText,CommandType.Text,strSql.ToString()));
        }
        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "Favorite";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        public DataSet GetPageList(int PageSize, int PageIndex, string userName, int languageID)
        {
            SqlParameter[] parameters = {
                   
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@UserName", SqlDbType.VarChar,50),
                    new SqlParameter("@LanguageID",SqlDbType.Int)
                    };
            parameters[0].Value = PageSize;
            parameters[1].Value = PageIndex;
            parameters[2].Value = userName;
            parameters[3].Value = languageID;
            return DbHelperSQL.RunProcedure("UP_Favorite_GetPageList", parameters, "ds");
        }

        #endregion  成员方法
    }
}

